package servlet;

import dao.BookDao;
import vo.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "InsertBookServlet")
public class InsertBookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        String id=new String(request.getParameter("id").getBytes("iso-8859-1"),"UTF-8");
        String name=new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
        String zz=new String(request.getParameter("zz").getBytes("iso-8859-1"),"utf-8");
        String cbs=new String(request.getParameter("cbs").getBytes("iso-8859-1"),"utf-8");
        Float jg= Float.valueOf(request.getParameter("jg"));
        Book book=new Book();
        book.setBook_number(id);
        book.setBook_name(name);
        book.setAothor(zz);
        book.setPublisher(cbs);
        book.setPrice(jg);

        BookDao dao=new BookDao();
        boolean i=dao.insert(book);
        if (i){
            request.getRequestDispatcher("booksservlet").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
